package com.demo.core.io;

import java.io.File;
import java.io.FileFilter;

/**
 * 扩展名过滤器
 * gyikv 
 * @author ryuu.kk
 *
 */
public class FileExtensionFilter implements FileFilter {

	/**
	 * 文件扩展名列表
	 */
	private String[] extensions;
	/**
	 * 扩展名是否包含
	 */
	private Boolean include;
	
	public FileExtensionFilter(String[] extensions, Boolean isInclude) {
		this.extensions = extensions;
		this.include = isInclude;
	}
	@Override
	public boolean accept(File file) {
		boolean allow = false;
		if (include == null) {
			return true;
		} 
		
		for (String e : extensions) {
			String name = file.getName();
			int index = name.lastIndexOf(".");
			if (index > 0) {
				allow = e.equals(name.substring(0, index - 1));
			} else {
				allow = e.equals(name);
			}
			if (allow) {
				break;
			}
		}
		return allow && include;
	}

}
